package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyerCarBean;
import com.project.bean.SellCarBean;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISellCarMapper;
import com.project.mapper.ISellMapper;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class SellCarServiceImpl implements ISellCarService {

    @Autowired
    private ISellCarMapper sellCarMapper;
    @Autowired
    private IProductMapper productMapper;
    @Override
    public List<SellCarBean> findByUserId(Integer userId) {
        return sellCarMapper.findByUserId(userId);
    }

    @Override
    public void add(String productId, Integer userId) {
        QueryWrapper<SellCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId",userId);
        qw.eq("fk_productId",productId);
        //查询指定用户ID和商品ID的购物信息
        SellCarBean sellCarBean = sellCarMapper.selectOne(qw);
        if (sellCarBean==null){//销售车中不存在该购物信息
            sellCarBean =new SellCarBean();
            sellCarBean.setUserId(userId);
            sellCarBean.setProductId(productId);
            sellCarBean.setNum(1);

            //保存购物信息
            sellCarMapper.insert(sellCarBean);
        }
        else {//销售车中存在该购物信息，则更新数量
            sellCarBean.setNum(sellCarBean.getNum()+1);
            //修改销售数量
            sellCarMapper.updateById(sellCarBean);
        }
    }

    @Override
    public void del(Integer sellCarId) {
        sellCarMapper.deleteById(sellCarId);
    }

    @Override
    public void updateNum(Integer sellCarId, Integer num) {
        SellCarBean sellCarBean = sellCarMapper.selectById(sellCarId);
        sellCarBean.setId(sellCarId);
        sellCarBean.setNum(num);
        sellCarMapper.updateById(sellCarBean);
    }

    @Override
    public Integer getTotalPrice(Integer userId) {
        return sellCarMapper.getTotalPage(userId);
    }
}
